package com.sis.edts.projectmanage.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.sis.base.dto.BaseResult;
import com.sis.base.util.RequestUtil;
import com.sis.edts.projectmanage.entity.Project;
import com.sis.edts.projectmanage.service.IProjectService;
import com.xiaoleilu.hutool.date.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author cutegl
 * @since 2017-08-07
 */
@Controller
@RequestMapping("/projectmanage/project")
@Api(value = "项目接口", description = "项目接口")
public class ProjectController {
    private final static Logger logger = LoggerFactory.getLogger(ProjectController.class);
    @Autowired
    private IProjectService projectService;

    @ApiOperation(value = "新建项目", httpMethod = "POST")
    @RequestMapping(value = "/newProject", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult<Project> newProject(@RequestBody Project project, HttpServletRequest request) {
        boolean isSuccess = true;
        try {
            project.setIp(RequestUtil.getIpAddr(request));
            project.setLastUpdateDate(project.getCreateDate());
            isSuccess = projectService.insert(project);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new BaseResult<>(isSuccess, "新建项目失败");
        }
        return new BaseResult<>(isSuccess, "新建项目成功", project);
    }

    @ApiOperation(value = "修改项目", httpMethod = "POST")
    @RequestMapping(value = "/updateProject", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult<Project> updateProject(@RequestBody Project project, HttpServletRequest request) {
        boolean isSuccess = true;
        try {
            project.setLastUpdator(RequestUtil.getIpAddr(request));
            project.setLastUpdateDate(DateUtil.date());
            isSuccess = projectService.updateById(project);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new BaseResult<>(isSuccess, "修改项目失败");
        }
        return new BaseResult<>(isSuccess, "修改项目成功", project);
    }

    @ApiOperation(value = "删除项目", httpMethod = "POST")
    @RequestMapping(value = "/deleteProject", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult<Project> deleteProject(@RequestBody List<Integer> ids) {
        boolean isSuccess = true;
        try {
            isSuccess = projectService.deleteBatchIds(ids);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new BaseResult<>(isSuccess, "删除项目失败");
        }
        return new BaseResult<>(isSuccess, "删除项目成功");
    }

    @ApiOperation(value = "项目列表", httpMethod = "GET")
    @RequestMapping(value = "/projectList", method = RequestMethod.GET)
    @ResponseBody
    public BaseResult<List<Project>> projectList() {
        List<Project> list;
        try {
            list = projectService.selectList(null);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new BaseResult<>(false, "查询项目失败");
        }
        return new BaseResult<>(true, list);
    }

    @ApiOperation(value = "查询项目", httpMethod = "GET")
    @RequestMapping(value = "/queryProject", method = RequestMethod.GET)
    @ResponseBody
    public BaseResult<List<Project>> queryProject(@RequestParam String  projectName) {
        List<Project> list;
        try {
            EntityWrapper<Project> entityWrapper = new EntityWrapper<>();
            entityWrapper.like("project_name", projectName);
            list = projectService.selectList(entityWrapper);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new BaseResult<>(false, "查询项目失败");
        }
        return new BaseResult<>(true, list);
    }
}
